package com.ecoolex.chinadutyfree.retailer.core.manager;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ecoolex.chinadutyfree.retailer.core.dao.DirectMailGoodsOrderDetailDAO;
import com.ecoolex.chinadutyfree.retailer.core.pojo.GoodsOrderDetailDO;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrder;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderDetail;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderDetailExample;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsOrderShippedRecord;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DirectMailGoodsOrderDetailManager {
	
    @Autowired
    private DirectMailGoodsOrderDetailDAO dao;

    public DirectMailGoodsOrderDetail add(DirectMailGoodsOrder order, GoodsOrderDetailDO detailDo, String createUser) {
        BigDecimal taxRate = BigDecimal.ZERO;
        BigDecimal taxPrice = BigDecimal.ZERO;
        if(Check.notNullAndGtZero(detailDo.getTaxRate())){
            taxRate = detailDo.getTaxRate().divide(BigDecimal.valueOf(100));
            taxPrice = detailDo.getCart().getRetailPrice().multiply(taxRate).multiply(BigDecimal.valueOf(detailDo.getCart().getBuyNumber()));
        }
        DirectMailGoodsOrderDetail detail = new DirectMailGoodsOrderDetail();
        detail.setOrderId(order.getOrderId());
        detail.setGoodsId(detailDo.getCart().getGoodsId());
        detail.setDirectMailGoodsId(detailDo.getCart().getDirectMailGoodsId());
        detail.setProductId(detailDo.getCart().getProductId());
        detail.setDirectMailProductId(detailDo.getCart().getDirectMailProductId());
        detail.setName(detailDo.getCart().getName());
        detail.setSpecNames(detailDo.getCart().getSpecNames());
        detail.setImage(detailDo.getCart().getImage());
        detail.setBuyNumber(detailDo.getCart().getBuyNumber());
        detail.setRetailPrice(detailDo.getCart().getRetailPrice());
        detail.setTaxRate(Optional.ofNullable(detailDo.getTaxRate()).orElse(BigDecimal.ZERO));
        detail.setTaxPrice(taxPrice);
        detail.setTotalPrice(detailDo.getCart().getTotalPrice());
        detail.setFreightTemplateId(detailDo.getFreightTemplateChargingRegion().getFreightTemplateId());
        detail.setDeleted(DirectMailGoodsOrderDetail.NOT_DELETED);
        detail.setCreateUser(createUser);
        detail.setCreateTime(Date.from(Instant.now()));
        detail.setUpdateUser(createUser);
        detail.setUpdateTime(Date.from(Instant.now()));
        dao.insert(detail);
        return detail;
    }

    public List<DirectMailGoodsOrderDetail> queryByOrderId(Integer orderId) {
        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        example.createCriteria().andOrderIdEqualTo(orderId).andDeletedEqualTo(DirectMailGoodsOrderDetail.NOT_DELETED);
        return dao.selectByExample(example);
    }
    
    public List<DirectMailGoodsOrderDetail> queryOrderDetailPossibleBeDeleted(Integer orderId) {
        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        example.createCriteria().andOrderIdEqualTo(orderId);
        return dao.selectByExample(example);
    }
    
    public long countShippedDetailByOrderId(Integer orderId) {
        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        DirectMailGoodsOrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andShippedRecordIdIsNotNull().andDeletedEqualTo(
            DirectMailGoodsOrderDetail.NOT_DELETED);
        return dao.countByExample(example);
    }


    public void deleteByOrderId(DirectMailGoodsOrder order, String updateUser) {
        DirectMailGoodsOrderDetail record = new DirectMailGoodsOrderDetail();
        record.setDeleted(DirectMailGoodsOrderDetail.IS_DELETED);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        example.createCriteria()
                .andOrderIdEqualTo(order.getOrderId())
                .andDeletedEqualTo(DirectMailGoodsOrderDetail.NOT_DELETED);

        dao.updateByExampleSelective(record, example ,
                DirectMailGoodsOrderDetail.Column.deleted, DirectMailGoodsOrderDetail.Column.updateUser, DirectMailGoodsOrderDetail.Column.updateTime);
    }

    public DirectMailGoodsOrderDetail getByOrderId(Integer orderId, Integer detailId) {
        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        example.createCriteria().andDetailIdEqualTo(detailId).andOrderIdEqualTo(orderId).andDeletedEqualTo(
                DirectMailGoodsOrderDetail.NOT_DELETED);
        return dao.selectOneByExample(example);
    }

    public boolean shipped(DirectMailGoodsOrderShippedRecord shippedRecord, DirectMailGoodsOrderDetail detail, String updateUser) {
        detail.setShippedRecordId(shippedRecord.getShippedRecordId());
        detail.setUpdateUser(updateUser);
        detail.setUpdateTime(Date.from(Instant.now()));
        return dao.updateByPrimaryKeySelective(detail,
                DirectMailGoodsOrderDetail.Column.shippedRecordId,
                DirectMailGoodsOrderDetail.Column.updateUser,
                DirectMailGoodsOrderDetail.Column.updateTime) == 1;
    }

    public void cleanShippedRecordId(Integer orderId, Integer shippedRecordId, List<Integer> notIncludeIdList, String updateUser) {
        DirectMailGoodsOrderDetail record = new DirectMailGoodsOrderDetail();
        record.setShippedRecordId(null);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        DirectMailGoodsOrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andShippedRecordIdEqualTo(shippedRecordId).andDeletedEqualTo(
                DirectMailGoodsOrderDetail.NOT_DELETED);
        if (Check.notNullOrEmpty(notIncludeIdList)) {
            criteria.andDetailIdNotIn(notIncludeIdList);
        }

        dao.updateByExampleSelective(record, example,
                DirectMailGoodsOrderDetail.Column.shippedRecordId, DirectMailGoodsOrderDetail.Column.updateUser, DirectMailGoodsOrderDetail.Column.updateTime);
    }
    
    public void cleanAllShippedRecordId(Integer orderId,String updateUser) {
    	DirectMailGoodsOrderDetail record = new DirectMailGoodsOrderDetail();
        record.setShippedRecordId(null);
        record.setUpdateUser(updateUser);
        record.setUpdateTime(Date.from(Instant.now()));

        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        DirectMailGoodsOrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(DirectMailGoodsOrderDetail.NOT_DELETED);

        dao.updateByExampleSelective(record, example,
                DirectMailGoodsOrderDetail.Column.shippedRecordId, DirectMailGoodsOrderDetail.Column.updateUser, DirectMailGoodsOrderDetail.Column.updateTime);
    }

    public long countNotShippedDetailByOrderId(Integer orderId) {
        DirectMailGoodsOrderDetailExample example = new DirectMailGoodsOrderDetailExample();
        DirectMailGoodsOrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId) //
                .andShippedRecordIdIsNull() //
                .andDeletedEqualTo(DirectMailGoodsOrderDetail.NOT_DELETED);
        return dao.countByExample(example);
    }
    
    public List<DirectMailGoodsOrderDetail> listByDirectMailGoodsProductId(Integer directMailProductId,Integer userId,Date startTime,Date endTime){
    	return dao.listByDirectMailGoodsProductId(directMailProductId,userId, startTime, endTime);
    }
}